#include <string.h>
#include "ASControl.h"
#include "ASLabel.h"
#include "ASWaiting.h"
#include "ASImage.h"
#include "ASScrollText.h"
#include "ASHyperlink.h"
#include "ASButton.h"
#include "ASTableBar.h"
#include "ASListItem.h"
#include "ASList.h"
#include "ASProgressBar.h"
#include "ASRank.h"
#include "ASFilledArea.h"
#include "ASAdvbar.h"
#include "ASTextView.h"
#include "ASScrollBar.h"


as_control* as_ctrl_create(as_control_type type, as_rect area,
                           ASINT extra_param_cnts, ASVOID** extra_param_p)
{
    as_control* ctrl_p = NULL;

    switch (type)
    {
        case AS_CTRL_LABEL:
            if(extra_param_cnts == 1)
            {
                ctrl_p = (as_control*)as_ctrl_label_create(area, (ASWSTR)extra_param_p[0]);
            }

            break;

        case AS_CTRL_WAITING:
            if(extra_param_cnts == 1)
            {
                ctrl_p = (as_control*)as_ctrl_waiting_create(area, (as_waiting_type)(ASINT)extra_param_p[0]);
            }

            break;

        case AS_CTRL_IMAGE:
            if(extra_param_cnts == 3)
            {
                ctrl_p = (as_control*)as_ctrl_image_create(area,
                         (as_image_src_type)(ASINT)extra_param_p[0], (ASWSTR)extra_param_p[1], (ASINT)extra_param_p[2]);
            }

            break;

        case AS_CTRL_SCROLLTEXT:
            if(extra_param_cnts == 3)
            {
                ctrl_p = (as_control*)as_ctrl_scrolltext_create(area,
                         (ASWSTR)extra_param_p[0], (ASUINT)extra_param_p[1], (ASUINT)extra_param_p[2]);
            }

            break;

        case AS_CTRL_HYPERLINK:
            if(extra_param_cnts == 2)
            {
                ctrl_p = (as_control*)as_ctrl_hyperlink_create(area, (ASWSTR)extra_param_p[0], (ASCHAR*)extra_param_p[1]);
            }

            break;

        case AS_CTRL_BUTTON:
            if(extra_param_cnts == 5)
            {
                ctrl_p = (as_control*)as_ctrl_button_create(area,
                         (as_button_type)(ASUINT)extra_param_p[0], (ASWSTR)extra_param_p[1], (ASWSTR)extra_param_p[2],
                         (ASWSTR)extra_param_p[3], (ASUINT)extra_param_p[4]);
            }

            break;

        case AS_CTRL_TABLEBAR:
            if(extra_param_cnts == 3)
            {
                ctrl_p = (as_control*)as_ctrl_tablebar_create(area,
                         (ASINT)extra_param_p[0], (as_tablebar_src_type)(ASUINT)extra_param_p[1], (as_tablebar_show_type)(ASUINT)extra_param_p[2]);
            }

            break;

        case AS_CTRL_LISTITEM:
            if(extra_param_cnts == 1)
            {
                ctrl_p = (as_control*)as_ctrl_listitem_create(area, (as_rect*)extra_param_p[0]);
            }

            break;

        case AS_CTRL_LIST:
            if(extra_param_cnts == 0)
            {
                ctrl_p = (as_control*)as_ctrl_list_create(area);
            }

            break;

        case AS_CTRL_PROGRESSBAR:
            if(extra_param_cnts == 1)
            {
                ctrl_p = (as_control*)as_ctrl_progressbar_create(area, (as_progressbar_type)(ASUINT)extra_param_p[0]);
            }

            break;

        case AS_CTRL_RANKING:
            if(extra_param_cnts == 4)
            {
                ctrl_p = (as_control*)as_ctrl_rank_create(area, (as_image_src_type)(ASUINT)extra_param_p[0],
                         (ASWSTR)extra_param_p[1], (ASWSTR)extra_param_p[2], (ASINT)extra_param_p[3]);
            }

            break;

        case AS_CTRL_FILLEDAREA:
            if(extra_param_cnts == 0)
            {
                ctrl_p = (as_control*)as_ctrl_filledarea_create(area);
            }

            break;

        case AS_CTRL_ADVBAR:
            if(extra_param_cnts == 2)
            {
                ctrl_p = (as_control*)as_ctrl_advbar_create(area, (ASUINT)extra_param_p[0], (ASBOOL)extra_param_p[1]);
            }

            break;

        case AS_CTRL_TEXTVIEW:
            if(extra_param_cnts == 1)
            {
                ctrl_p = (as_control*)as_ctrl_textview_create(area, (ASWSTR)extra_param_p[0]);
            }

            break;

        case AS_CTRL_SCROLLBAR:
            if(extra_param_cnts == 1)
            {
                ctrl_p = (as_control*)as_ctrl_scrollbar_create(area, (as_scrollbar_layout)(ASUINT)extra_param_p[0]);
            }

            break;

        default:
            break;
    }

    return ctrl_p;
}

as_ret as_ctrl_destroy(as_control* ctrl_p)
{
    as_control_type type;
    as_ret ret = AS_OK;

    if(!ctrl_p)
    {
        ret = AS_INVALID_PARAM;
        return ret;
    }

    type = ctrl_p->ctrl_type;

    switch(type)
    {
        case AS_CTRL_LABEL:
            return as_ctrl_label_destroy((as_label*)ctrl_p);

        case AS_CTRL_WAITING:
            return as_ctrl_waiting_destroy((as_waiting*)ctrl_p);

        case AS_CTRL_IMAGE:
            return as_ctrl_image_destroy((as_image*)ctrl_p);

        case AS_CTRL_SCROLLTEXT:
            return as_ctrl_scrolltext_destroy((as_scrolltext*)ctrl_p);

        case AS_CTRL_HYPERLINK:
            return as_ctrl_hyperlink_destroy((as_hyperlink*)ctrl_p);

        case AS_CTRL_BUTTON:
            return as_ctrl_button_destroy((as_button*)ctrl_p);

        case AS_CTRL_TABLEBAR:
            return as_ctrl_tablebar_destroy((as_tablebar*)ctrl_p);

        case AS_CTRL_LISTITEM:
            return as_ctrl_listitem_destroy((as_listitem*)ctrl_p);

        case AS_CTRL_LIST:
            return as_ctrl_list_destroy((as_list*)ctrl_p);

        case AS_CTRL_PROGRESSBAR:
            return as_ctrl_progressbar_destory((as_progressbar*)ctrl_p);

        case AS_CTRL_RANKING:
            return as_ctrl_rank_destroy((as_rank*)ctrl_p);

        case AS_CTRL_FILLEDAREA:
            return as_ctrl_filledarea_destroy((as_filled_area*)ctrl_p);

        case AS_CTRL_ADVBAR:
            return as_ctrl_advbar_destroy((as_advbar*)ctrl_p);

        case AS_CTRL_TEXTVIEW:
            return as_ctrl_textview_destroy((as_textview*)ctrl_p);

        case AS_CTRL_SCROLLBAR:
            return as_ctrl_scrollbar_destory((as_scrollbar*)ctrl_p);

        default:
            return AS_INVALID_PARAM;
    }
}

static ASVOID as_ctrl_move(as_control* ctrl_p, ASINT x_off, ASINT y_off)
{
    if(!ctrl_p)
        return;

    ctrl_p->area.x += (ASINT16)x_off;
    ctrl_p->area.y += (ASINT16)y_off;
}

static ASVOID as_ctrl_set_hidden(as_control* ctrl_p, ASINT if_hidden)
{
    if(!ctrl_p)
        return;

    if(ctrl_p->if_hidden != if_hidden)
        ctrl_p->if_hidden = if_hidden;
}

static ASINT as_ctrl_get_hidden(as_control* ctrl_p)
{
    if(!ctrl_p)
        return ASTRUE;

    return ctrl_p->if_hidden;
}
static ASVOID as_ctrl_change_area(as_control* ctrl_p, as_rect area)
{
    if(!ctrl_p)
        return;

    memcpy(&ctrl_p->area, &area, sizeof(as_rect));
}

ASVOID as_ctrl_init(as_control* ctrl_p, as_rect area)
{
    if(!ctrl_p)
        return;

    memcpy(&ctrl_p->area, &area, sizeof(as_rect));
    ctrl_p->show_hdlr = 0/*as_get_default_show_hdlr()*/;
    ctrl_p->change_area_hdlr = as_ctrl_change_area;
    ctrl_p->move = as_ctrl_move;
    ctrl_p->set_hidden = as_ctrl_set_hidden;
    ctrl_p->get_hidden = as_ctrl_get_hidden;
    ctrl_p->pen_hdlr = NULL;
    ctrl_p->key_hdlr = NULL;
}

//just for control flush immediately
ASVOID as_ctrl_flush(ASVOID)
{
    as_flush();
}